package by.amakarevich.services.impl;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.ProductDao;
import by.amakarevich.dataaccess.ThreadDao;
import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.Thread;
import by.amakarevich.services.ThreadService;

@Service
public class ThreadServiceImpl implements ThreadService {

	@Inject
	private ThreadDao threadDao;

	@Inject
	private ProductDao productDao;

	@Override
	public void createNewThread(Thread thread, Product product) {
		Validate.isTrue(
				thread.getId() == null,
				"This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		thread.setProduct(product);
		threadDao.insert(thread);
	}

	@Override
	public Thread get(Long id) {
		return threadDao.getById(id);
	}

	@Override
	public void updateThread(Thread thread) {
		threadDao.update(thread);
	}

	@Override
	public void removeThread(Long id) {

		threadDao.delete(id);
		productDao.delete(id);

	}

	@Override
	public void deleteAll() {
		threadDao.deleteAll();
		productDao.deleteAll();
	}

	@Override
	public List<Thread> getAllThread() {
		return threadDao.getAllThread();
	}

	@Override
	public Thread getByProductId(Product product) {
		return threadDao.getByProductId(product);
	}
}
